package com.wyp168.juc.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @ClassName SaleTicket
 * @Description TODO
 * @Author wyp168
 * @Date 2022/9/4 10:09
 */
class LTicket {
    private Integer ticketCount = 50;
    //创建可重入锁
    private Lock lock = new ReentrantLock();

    public void sale() {
        lock.lock();
        try {
            if (ticketCount > 0) {
                System.out.println(Thread.currentThread().getName() + "卖出第" + ticketCount -- + "张票，剩余" + ticketCount + "张票");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
public class LSaleTicket {
    public static void main(String[] args) {
        LTicket lSaleTicket = new LTicket();
        new Thread(() -> {
            for (int i = 0; i < 80; i++) {
                lSaleTicket.sale();
            }
        }, "AA").start();

        new Thread(() -> {
            for (int i = 0; i < 80; i++) {
                lSaleTicket.sale();
            }
        }, "BB").start();

        new Thread(() -> {
            for (int i = 0; i < 80; i++) {
                lSaleTicket.sale();
            }
        }, "CC").start();
    }
}
